<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=589696
-->
<head>
  <title>Test for Bug 589696</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=589696">Mozilla Bug 589696</a>
<p id="display"></p>
<iframe style='width:50px; height: 50px;' name='t'></iframe>
<div id="content">
  <!-- Next forms should not submit because formnovalidate isn't set on the
       element used for the submission. -->
  <form target='t' action='data:text/html,'>
    <input id='av' required>
    <input type='submit' formnovalidate>
    <input id='a' type='submit'>
  </form>
  <form target='t' action='data:text/html,'>
    <input id='bv' type='checkbox' required>
    <button type='submit' formnovalidate></button>
    <button id='b' type='submit'></button>
  </form>
  <!-- Next form should not submit because formnovalidate only applies for
       submit controls. -->
  <form target='t' action='data:text/html,'>
    <input id='c' required formnovalidate>
  </form>
  <!--- Next forms should submit without any validation check. -->
  <form target='t' action='data:text/html,'>
    <input id='dv' required>
    <input id='d' type='submit' formnovalidate>
  </form>
  <form target='t' action='data:text/html,'>
    <input id='ev' type='checkbox' required>
    <button id='e' type='submit' formnovalidate></button>
  </form>
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 589696 **/

var os = SpecialPowers.Cc['@mozilla.org/observer-service;1']
                      .getService(SpecialPowers.Ci.nsIObserverService);
var observers = os.enumerateObservers("invalidformsubmit");

/**
 * formnovalidate should prevent form validation if set on the submit control
 * used to submit the form.
 *
 * The following test should not be done if there is no observer for
 * "invalidformsubmit" because the form submission will not be canceled in that
 * case.
 */

if (observers.hasMoreElements()) {
  document.getElementById('av').addEventListener("invalid", function(aEvent) {
    aEvent.target.removeAttribute("invalid", arguments.callee, false);
    ok(true, "formnovalidate should not apply on if not set on the submit " +
             "control used for the submission");
    document.getElementById('b').click();
  }, false);

  document.getElementById('bv').addEventListener("invalid", function(aEvent) {
    aEvent.target.removeAttribute("invalid", arguments.callee, false);
    ok(true, "formnovalidate should not apply on if not set on the submit " +
             "control used for the submission");
    var c = document.getElementById('c');
    c.focus();
    synthesizeKey("KEY_Enter", { code: "Enter" });
  }, false);

  document.getElementById('c').addEventListener("invalid", function(aEvent) {
    aEvent.target.removeAttribute("invalid", arguments.callee, false);
    ok(true, "formnovalidate should only apply on submit controls");
    document.getElementById('d').click();
  }, false);

  document.forms[3].addEventListener("submit", function(aEvent) {
    aEvent.target.removeAttribute("submit", arguments.callee, false);
    ok(true, "formnovalidate applies if set on the submit control used for the submission");
    document.getElementById('e').click();
  }, false);

  document.forms[4].addEventListener("submit", function(aEvent) {
    aEvent.target.removeAttribute("submit", arguments.callee, false);
    ok(true, "formnovalidate applies if set on the submit control used for the submission");
    SimpleTest.executeSoon(SimpleTest.finish);
  }, false);

  /**
   * We have to be sure invalid events behave as expected.
   * They should be sent before the submit event so we can just create a test
   * failure if we got one when unexpected. All of them should be caught if
   * sent.
   * At worst, we got random green which isn't harmful.
   * If expected, they will be part of the chain reaction.
   */
  function unexpectedInvalid(aEvent)
  {
    aEvent.target.removeAttribute("invalid", unexpectedInvalid, false);
    ok(false, "invalid event should not be sent");
  }

  document.getElementById('dv').addEventListener("invalid", unexpectedInvalid, false);
  document.getElementById('ev').addEventListener("invalid", unexpectedInvalid, false);

  /**
   * Some submission have to be canceled. In that case, the submit events should
   * not be sent.
   * Same behavior as unexpected invalid events.
   */
  function unexpectedSubmit(aEvent)
  {
    aEvent.target.removeAttribute("submit", unexpectedSubmit, false);
    ok(false, "submit event should not be sent");
  }

  document.forms[0].addEventListener("submit", unexpectedSubmit, false);
  document.forms[1].addEventListener("submit", unexpectedSubmit, false);
  document.forms[2].addEventListener("submit", unexpectedSubmit, false);

  SimpleTest.waitForExplicitFinish();

  // This is going to call all the tests (with a chain reaction).
  SimpleTest.waitForFocus(function() {
    document.getElementById('a').click();
  });
} else {
  todo(false, "No 'invalidformsubmit' observers. Skip test.");
}

</script>
</pre>
</body>
</html>
